package com.campus.common.dormitories.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.campus.api.util.UserContext;
import com.campus.common.dormitories.entity.Dormitories;
import com.campus.common.dormitories.mapper.DormitoriesMapper;
import com.campus.common.dormitories.service.IDormitoriesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.common.exception.ServiceException;
import com.campus.common.result.Result;
import com.campus.common.student.job.entity.Student;
import com.campus.common.student.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * <p>
 * 宿舍表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-01
 */
@Service
public class DormitoriesServiceImpl extends ServiceImpl<DormitoriesMapper, Dormitories> implements IDormitoriesService {

    @Autowired
    private DormitoriesMapper dormitoriesMapper;

    @Autowired
    private StudentMapper studentMapper;
    @Override
    public Result insertDor(String dormId) {

        Integer userId= UserContext.getUserId();
        Student student=studentMapper.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getUserId,userId));
        if (Optional.ofNullable(student).isEmpty()) {
            throw new ServiceException("未查询到该学生信息");
        }

        Dormitories dormitories=new Dormitories();
        dormitories.setDormId(dormId);
        dormitories.setStudentId(student.getId());
        int insert = dormitoriesMapper.insert(dormitories);
        if(insert<0){
            return Result.fail("绑定宿舍失败");
        }
        return Result.success();
    }
}
